﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Web.Script.Serialization;
using test;
using System.Data.Entity;

namespace test
{
    public class Repository<TEntity> : DataBase, IRepository<TEntity>
       where TEntity : class
    {
        public Repository(DbContext db)
            : base(db)
        { }

        public virtual IQueryable<TEntity> GetEntities()
        {
            return base.EF_DB.Set<TEntity>();
        }

        public virtual void Add(TEntity entity)
        {
            EF_DB.Entry<TEntity>(entity);
            EF_DB.Set<TEntity>().Add(entity);
        }

        public virtual void Add(List<TEntity> entities)
        {
            entities.ForEach(entity =>
            {
                this.Add(entity);
            });
        }

        public virtual void Delete(TEntity entity)
        {
            EF_DB.Set<TEntity>().Attach(entity);
            EF_DB.Set<TEntity>().Remove(entity);
        }

        public virtual void Delete(List<TEntity> entities)
        {
            entities.ForEach(entity =>
            {
                this.Delete(entity);
            });
        }

        public virtual void Update(TEntity entity)
        {
            EF_DB.Set<TEntity>().Attach(entity);

        }

        public virtual void Update(List<TEntity> entities)
        {
            entities.ForEach(entity =>
            {
                this.Update(entity);
            });
        }


    }
}
